package cn.wuxing.mediation.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.wuxing.mediation.common.exception.ServiceException;
import cn.wuxing.mediation.common.utils.DateUtils;
import cn.wuxing.mediation.common.utils.StringUtils;
import cn.wuxing.mediation.system.domain.LeadDutyArrangement;
import cn.wuxing.mediation.system.domain.ShiftScheduling;
import cn.wuxing.mediation.system.domain.VisitingReception;
import cn.wuxing.mediation.system.mapper.LeadDutyArrangementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wuxing.mediation.system.service.ILeadDutyArrangementService;

import javax.annotation.Resource;

/**
 * 领导值班安排Service业务层处理
 *
 * @author mediation
 * @date 2024-10-12
 */
@Service
public class LeadDutyArrangementServiceImpl implements ILeadDutyArrangementService
{
    @Resource
    private LeadDutyArrangementMapper leadDutyArrangementMapper;

    /**
     * 查询领导值班安排
     *
     * @param id 领导值班安排主键
     * @return 领导值班安排
     */
    @Override
    public LeadDutyArrangement selectLeadDutyArrangementById(Long id)
    {
        return leadDutyArrangementMapper.selectLeadDutyArrangementById(id);
    }

    /**
     * 查询领导值班安排列表
     *
     * @param leadDutyArrangement 领导值班安排
     * @return 领导值班安排
     */
    @Override
    public List<LeadDutyArrangement> selectLeadDutyArrangementList(LeadDutyArrangement leadDutyArrangement)
    {
        return leadDutyArrangementMapper.selectLeadDutyArrangementList(leadDutyArrangement);
    }

    /**
     * 新增领导值班安排
     *
     * @param leadDutyArrangement 领导值班安排
     * @return 结果
     */
    @Override
    public int insertLeadDutyArrangement(LeadDutyArrangement leadDutyArrangement)
    {
        leadDutyArrangement.setCreateTime(DateUtils.getNowDate());
        leadDutyArrangement.setDelFlag("0");
        return leadDutyArrangementMapper.insertLeadDutyArrangement(leadDutyArrangement);
    }

    /**
     * 修改领导值班安排
     *
     * @param leadDutyArrangement 领导值班安排
     * @return 结果
     */
    @Override
    public int updateLeadDutyArrangement(LeadDutyArrangement leadDutyArrangement)
    {
        leadDutyArrangement.setUpdateTime(DateUtils.getNowDate());
        return leadDutyArrangementMapper.updateLeadDutyArrangement(leadDutyArrangement);
    }

    /**
     * 批量删除领导值班安排
     *
     * @param ids 需要删除的领导值班安排主键
     * @return 结果
     */
    @Override
    public int deleteLeadDutyArrangementByIds(Long[] ids)
    {
        return leadDutyArrangementMapper.deleteLeadDutyArrangementByIds(ids);
    }

    /**
     * 删除领导值班安排信息
     *
     * @param id 领导值班安排主键
     * @return 结果
     */
    @Override
    public int deleteLeadDutyArrangementById(Long id)
    {
        return leadDutyArrangementMapper.deleteLeadDutyArrangementById(id);
    }

    @Override
    public String importShiftScheduling(List<LeadDutyArrangement> leadDutyArrangementList, boolean updateSupport) {
        if (StringUtils.isNull(leadDutyArrangementList) || leadDutyArrangementList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LeadDutyArrangement leadDutyArrangement : leadDutyArrangementList) {
            try {
                // 验证庭室名称是否重复
                // 验证庭室名称是否重复
                if (leadDutyArrangement!=null) {
                    leadDutyArrangement.setDelFlag("0");
                    leadDutyArrangement.setCreateTime(new Date());
                    leadDutyArrangementMapper.insertLeadDutyArrangement(leadDutyArrangement);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、姓名" + leadDutyArrangement.getName() + " 导入成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、姓名 " + leadDutyArrangement.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
